﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using shfa.snomtastic.systray.components;

namespace shfa.snomtastic.systray
{
    public class RegSettings
    {

        private RegistryKey UserPolicyKeyRoot = Registry.CurrentUser.OpenSubKey("Software\\Policies\\SHFA\\Snomtastic");
        private RegistryKey UserKeyRoot = Registry.CurrentUser.OpenSubKey("Software\\SHFA\\Snomtastic", true);
        //private static RegistryKey UserKeyRoot64 = Registry.CurrentUser.OpenSubKey("Wow6432Node\\SHFA\\Snomtastic", true);

        private  RegistryKey MachinePolicyKeyRoot = Registry.LocalMachine.OpenSubKey("Software\\Policies\\SHFA\\Snomtastic");
        private  RegistryKey MachineKeyRoot = Registry.LocalMachine.OpenSubKey("Software\\SHFA\\Snomtastic");
        private  RegistryKey MachineKeyRoot64 = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\SHFA\\Snomtastic");


        public  void setHideCommunicatorToastMode(HideToastInfo.HideToastModeType mode)
        {
            if (UserKeyRoot==null)
                UserKeyRoot = Registry.CurrentUser.CreateSubKey("Software\\SHFA\\Snomtastic");

            UserKeyRoot.SetValue("HideCommunicatorToastMode", ((int)mode).ToString());
        }

        public  void setHideSnomtasticToastMode(HideToastInfo.HideToastModeType mode)
        {
            if (UserKeyRoot == null)
                UserKeyRoot = Registry.CurrentUser.CreateSubKey("Software\\SHFA\\Snomtastic");


            UserKeyRoot.SetValue("HideSnomtasticToastMode", ((int)mode).ToString());
        }
        
        public  HideToastInfo.HideToastModeType hideCommunicatorToastMode()
        {
            return getToastModeFromKey("HideCommunicatorToastMode", hideCommunicatorToastDisabled());
        }

        public HideToastInfo.HideToastModeType hideSnomtasticToastMode()
        {
            return getToastModeFromKey("HideSnomtasticToastMode", hideSnomtasticToastDisabled());
        }




        public bool hideCommunicatorToastDisabled()
        {
            return (getUserKeyValue("HideCommunicatorToastDisabled", true) == "1");
        }

        public bool hideSnomtasticToastDisabled()
        {
            return (getUserKeyValue("HideSnomtasticToastDisabled", true) == "1");
        }


        public  string userServiceUrl()
        {


            string sUserServiceUrl = getMachineKeyValue("UserServiceURL", true);
            if (sUserServiceUrl != string.Empty)
            {
                return sUserServiceUrl + ((sUserServiceUrl.LastIndexOf("/") == sUserServiceUrl.Length - 1) ? "" : "/") + "/services/user.asmx";
            }

            return "/services/user.asmx";

            


        }

        public int ValidateInterval()
        {

            string sInteval = getMachineKeyValue("ValidateInterval", true);
            Int32 interval = 300;
            if (Int32.TryParse(sInteval, out interval))
                return interval * 1000;

            return 300000;



        }


        #region Helpers

        private HideToastInfo.HideToastModeType getToastModeFromKey(string keyName, bool usePolicy)
        {
            string sKey = getUserKeyValue(keyName, usePolicy);

            if (sKey != string.Empty)
            {
                int iPolicy = 0;
                if (int.TryParse(sKey, out iPolicy))
                    return (HideToastInfo.HideToastModeType)iPolicy;
            }

            return HideToastInfo.HideToastModeType.Never;

        }

        private string getUserKeyValue(string keyName, bool usePolicy)
        {

            if (usePolicy)
                if (UserPolicyKeyRoot != null)
                    if (UserPolicyKeyRoot.GetValue(keyName) != null)
                        if (UserPolicyKeyRoot.GetValue(keyName).ToString() != string.Empty)
                            return UserPolicyKeyRoot.GetValue(keyName).ToString();

            if (UserKeyRoot != null)
                if (UserKeyRoot.GetValue(keyName) != null)
                    if (UserKeyRoot.GetValue(keyName).ToString() != string.Empty)
                        return UserKeyRoot.GetValue(keyName).ToString();

            return string.Empty;

        }


        private string getMachineKeyValue(string keyName, bool usePolicy)
        {
            if (usePolicy)
                if (MachinePolicyKeyRoot != null)
                    if (MachinePolicyKeyRoot.GetValue(keyName) != null)
                        if (MachinePolicyKeyRoot.GetValue(keyName).ToString() != string.Empty)
                            return MachinePolicyKeyRoot.GetValue(keyName).ToString();

            if (MachineKeyRoot != null)
                if (MachineKeyRoot.GetValue(keyName) != null)
                    if (MachineKeyRoot.GetValue(keyName).ToString() != string.Empty)
                        return MachineKeyRoot.GetValue(keyName).ToString();

            if (MachineKeyRoot64 != null)
                if (MachineKeyRoot64.GetValue(keyName) != null)
                    if (MachineKeyRoot64.GetValue(keyName).ToString() != string.Empty)
                        return MachineKeyRoot64.GetValue(keyName).ToString();

            return string.Empty;

        }





        #endregion


    }
}
